package com.wc.算法提高课.C第三章_图论.二分图.关押罪犯;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/22 20:56
 * @description https://www.acwing.com/problem/content/description/259/
 */
public class Main {
    /**
     * 思路： 二分 + 染色法<p>
     * 如果能将所有边权 > mid的人分为两个部分, 那就说明可以缩小范围
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 20010, M = 200010;
    static int[] h = new int[N], e = new int[M], ne = new int[M], w = new int[M];
    static int[] color = new int[N];
    static int n, m, idx = 1;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();
            add(a, b, c);
            add(b, a, c);
        }
        int l = 0, r = (int) 1e9;
        while (l < r) {
            int mid = l + r >> 1;
            Arrays.fill(color, 1, n + 1, 0);
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        out.println(r);
        out.flush();
    }

    static boolean dfs(int u, int c, int mid) {
        color[u] = c;
        for (int i = h[u]; i > 0; i = ne[i]) {
            int j = e[i];
            if (w[i] <= mid) continue;
            if (color[j] == 0){
                if (!dfs(j, 3 - c, mid)) return false;
            } else if (color[j] == c) return false;
        }
        return true;
    }

    static boolean check(int mid) {
        for (int i = 1; i <= n; i++) {
            if (color[i] == 0) {
                if (!dfs(i, 1, mid)) return false;
            }
        }
        return true;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        w[idx] = c;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
